// 处理data单数处理为get请求字符串
export const buildGetRequest = (baseUrl: string, params: Record<string, string>): string => {
  const urlSearchParams = new URLSearchParams()
  for (const [key, value] of Object.entries(params)) {
    urlSearchParams.append(key, value)
  }
  return `${baseUrl}?${urlSearchParams.toString()}`
}

// 计算距离
export const getDistance = (la1: number, lo1: number, la2: number, lo2: number) => {
  // 当前的纬度，当前的经度，接口拿到的纬度，接口拿到的经度
  const La1 = (la1 * Math.PI) / 180.0
  const La2 = (la2 * Math.PI) / 180.0
  const La3 = La1 - La2
  const Lb3 = (lo1 * Math.PI) / 180.0 - (lo2 * Math.PI) / 180.0
  let distance =
    2 *
    Math.asin(
      Math.sqrt(
        Math.pow(Math.sin(La3 / 2), 2) +
          Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2),
      ),
    )
  distance = distance * 6378.137
  distance = Math.round(distance * 10000) / 10000
  return distance
}

/**
 * 处理 北京市北京市丰台区北大地四里甲12号楼(近丰台文化馆,地铁9号线丰台东大街站)
 *
 * ['北京市','北京市','丰台区','北大地四里甲12号楼(近丰台文化馆,地铁9号线丰台东大街站)]
 */
export const getArea = (str: string) => {
  const reg = /.+?(省|市|自治区|自治州|县|区)/g
  const extract = str.match(reg)
  extract!.push(str.replace(reg, ''))
  return extract
}

export const isFull18Years = (birthDateString: string) => {
  const year = birthDateString.slice(0, 4)
  const mon = birthDateString.slice(4, 6)
  const day = birthDateString.slice(6, 8)
  const birthDateStrings = year + '-' + mon + '-' + day
  // 将出生日期字符串转换为Date对象
  const birthDate = new Date(birthDateStrings);
  // 获取当前日期
  const currentDate = new Date();

  // 计算当前年份与出生年份的差
  let yearsDifference = currentDate.getFullYear() - birthDate.getFullYear();

  // 如果当前日期还没到出生年份的下一个年份，则减去1
  if (currentDate.getMonth() < birthDate.getMonth() ||
      (currentDate.getMonth() === birthDate.getMonth() && currentDate.getDate() < birthDate.getDate())) {
      yearsDifference--;
  }

  // 返回是否满18周岁的结果
  return yearsDifference >= 18;
}
